﻿using System;
using System.Xml.Linq;
using System.Drawing;
using System.Windows.Forms;
using Dimine.Core;
using Dimine.Entity;

namespace TestWinForm
{
    public partial class BlockViewer : Form
    {
        private ListView m_lstAttribute;
        private Button m_btnListAttribute;
        private Label label1;
        private Label label2;
        private static dmBlockDataCS m_block;
        public BlockViewer()
        {
            Init();
        }

        private void DealDMB(string filename)
        {
            m_block = new dmBlockDataCS();
            if (m_block.Open(filename) ==0)
                return;
            dmBlockFieldTypeCollectionCS fields = m_block.FieldsType;

            //for (int i = 0; i < fields.Count; ++i)
            //{
            //    dmBlockFieldTypeCS field = fields[i];
            //    m_lstAttribute.Columns.Add(field.Name);
            //}
            int count = m_block.CellCount;
            lbl.Text = "字段数:" + fields.Count + "            三角面片数:" + m_block.CellCount;
            /*dmOCTreeAddrCS cursor = new dmOCTreeAddrCS();
            if (m_block.InitCursor(cursor))
            {
                do
                {
                    ListViewItem item = null;
                    string name = fields[0].Name;
                    double value = 0;
                    if (!m_block.GetCursor(name))
                    {
                        item = m_lstAttribute.Items.Add("");
                        continue;
                    }
                    else
                    {
                        value = m_block.ExtractOctantValue(name);
                        item = m_lstAttribute.Items.Add(value + "");
                    }
                    for (int i = 1; i < fields.Count; ++i)
                    {
                        name = fields[i].Name;
                        if (!m_block.GetCursor(name))
                        {
                            item.SubItems.Add("");
                            continue;
                        }
                        value = m_block.ExtractOctantValue(name);
                        item.SubItems.Add(value + "");
                    }
                    if (m_lstAttribute.Items.Count > 1000)
                        break;
                } while (m_block.AdvCursor());
            }*/
            m_block.Close();
        }
        private void DealDMC(string filename)
        {
            dmConsBlockDataCS block = new dmConsBlockDataCS();
            if (block.Load(filename))
            {
               dmBlockAttributeCollectionCS atts=  block.QueryCell(0, 0, 0);
               if (atts.Count > 0)
                   MessageBox.Show("打开成功");
               else
                   MessageBox.Show("存在异常");
            }
            else
            {
                MessageBox.Show("文件打开失败!");
            }
        }
        private void DealDMG(string filename)
        {
            dmGeoDrillDataCS drill = new dmGeoDrillDataCS();
            if (drill.Load(filename))
            {
                double []bounds=drill.GetBounds();
                
                lbl.Text = "边界(" + bounds[0] + "," + bounds[1] + "),(" + bounds[2] + "," + bounds[3] + "),(" + bounds[4] + "," + bounds[5] + ")";
                //label1.Text = "Collar字段数:" + drill.GetCollarTable().GetFieldCount() + ",Survey字段数:" + drill.GetSurveyTable().GetFieldCount();
            }
            else
            {
                MessageBox.Show("文件打开失败!");
            }
        }
        private void DealBlock(string filename)
        {
            string ext = System.IO.Path.GetExtension(filename).ToLower();
            switch (ext)
            {
                case ".dmb":
                    DealDMB(filename);
                    break;
                case ".dmc":
                    DealDMC(filename);
                    break;
                case ".dmg":
                case ".dmd":
                case ".dmk":
                    DealDMG(filename);
                    break;
            }
            
        }
        private Label lbl;
        private void Init()
        {
            Text = "查看快段模型属性";

            //m_lstAttribute = new ListView();
            m_btnListAttribute = new Button();
            lbl = new Label();
            lbl.AutoSize = true;
            lbl.Location = new Point(0, 50);
            //m_lstAttribute.Dock = DockStyle.Bottom;
            //m_lstAttribute.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top | AnchorStyles.Bottom;
            //m_lstAttribute.Location = new Point(0, 40);
            //m_lstAttribute.Size = new Size(290, 225);
            //m_lstAttribute.View = View.Details;

            m_btnListAttribute.Location = new Point(15, 5);
            m_btnListAttribute.Size = new Size(75, 25);
            m_btnListAttribute.Text = "选择快段模型";
            m_btnListAttribute.UseVisualStyleBackColor = true;
            m_btnListAttribute.FlatStyle = FlatStyle.Popup;

            m_btnListAttribute.Click += (s, e) => {
                OpenFileDialog ofd = new OpenFileDialog();
                ofd.Title = "请选择一个快段模型文件";
                ofd.Filter = "Dimine文件(*.dmb;*.dmc;*.dmg;*.dmd;*.dmk)|*.dmb;*.dmc;*.dmg;*.dmd;*.dmk";
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    lbl.Text = "";
                    DealBlock(ofd.FileName);
                }
                //dmBlockDataCS blockEx = new dmBlockDataCS();
            };

            Controls.Add(m_btnListAttribute);
            Controls.Add(lbl);
            //Controls.Add(m_lstAttribute);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new BlockViewer());
        }

        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(40, 85);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(42, 143);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "label2";
            // 
            // BlockViewer
            // 
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "BlockViewer";
            this.ResumeLayout(false);
            this.PerformLayout();

        }
    }
}
